

package top.continew.admin.question.user.service.questionset.entity;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import top.continew.admin.question.model.question.Question;
import top.continew.admin.question.model.question.QuestionSet;
import top.continew.admin.question.model.question.QuestionSetQuestion;
import top.continew.admin.question.user.service.question.enums.QuestionTypeEnum;

import java.util.List;

@Slf4j
public class AbstractQuestionSet {
    @Getter
    protected QuestionSet questionSet;

    // 默认获取集合下的题目
    public List<Long> getQuestionIdList() {
        // 获取集合下的题目
        List<QuestionSetQuestion> questionSetQuestion = new QuestionSetQuestion().selectList(Wrappers
            .lambdaQuery(QuestionSetQuestion.class)
            .eq(QuestionSetQuestion::getQuestionSetId, this.questionSet.getId()));
        return questionSetQuestion.stream().map(QuestionSetQuestion::getQuestionId).toList();
    }

    // 默认获取集合下的题目
    public List<QuestionSetQuestion> getQuestionList() {
        // 获取集合下的题目
        List<QuestionSetQuestion> questionSetQuestion = new QuestionSetQuestion().selectList(Wrappers
            .lambdaQuery(QuestionSetQuestion.class)
            .eq(QuestionSetQuestion::getQuestionSetId, this.questionSet.getId()));
        return questionSetQuestion;
    }

    //获取题目数量
    public long getQuestionCount() {
        MPJLambdaWrapper<QuestionSetQuestion> wrapper = new MPJLambdaWrapper<QuestionSetQuestion>()
            .select("distinct t.question_id")
            .leftJoin(Question.class, Question::getId, QuestionSetQuestion::getQuestionId)
            .eq(Question::getOnlineStatus, 1L)
            .ne(Question::getType, QuestionTypeEnum.MATERIAL_QUESTIONS.getCode())
            .eq(QuestionSetQuestion::getQuestionSetId, this.questionSet.getId());
        return new QuestionSetQuestion().selectCount(wrapper);
    }

    public long getQuestionCount(Long specificationType) {
        MPJLambdaWrapper<QuestionSetQuestion> wrapper = new MPJLambdaWrapper<QuestionSetQuestion>()
            .select("distinct t.question_id")
            .leftJoin(Question.class, Question::getId, QuestionSetQuestion::getQuestionId)
            .eq(Question::getOnlineStatus, 1L)
            .ne(Question::getType, QuestionTypeEnum.MATERIAL_QUESTIONS.getCode())
            .eq(QuestionSetQuestion::getQuestionSetId, this.questionSet.getId())
            .eq(Question::getSpecificationType, specificationType);
        return new QuestionSetQuestion().selectCount(wrapper);
    }

    // 添加题目到集合
    public void addQuestion(Long questionSetId, Long questionId, Long fromId, String ext) {
        try {
            QuestionSetQuestion questionSetQuestion = new QuestionSetQuestion();
            questionSetQuestion.setQuestionSetId(questionSetId);
            questionSetQuestion.setQuestionId(questionId);
            questionSetQuestion.setStatus(1L);
            questionSetQuestion.setSort(System.currentTimeMillis());
            questionSetQuestion.setCtime(System.currentTimeMillis());
            questionSetQuestion.setUtime(System.currentTimeMillis());
            questionSetQuestion.setObjectId(fromId);
            questionSetQuestion.setExt(ext);
            questionSetQuestion.insert();
        } catch (Exception e) {
            log.info("重复添加");
        }
    }

    //创建集合
    public void createQuestionSet(QuestionSet questionSet) {
        // 创建集合
        questionSet.setCtime(System.currentTimeMillis());
        questionSet.setUtime(System.currentTimeMillis());
        questionSet.insert();
        this.questionSet = questionSet;
    }

    // 删除题目
    public void deleteQuestion(Long questionSetId, Long questionId) {
        // 删除题目
        QuestionSetQuestion questionSetQuestion = new QuestionSetQuestion().selectOne(Wrappers
            .lambdaQuery(QuestionSetQuestion.class)
            .eq(QuestionSetQuestion::getQuestionSetId, questionSetId)
            .eq(QuestionSetQuestion::getQuestionId, questionId));
        if (questionSetQuestion != null) {
            questionSetQuestion.deleteById();
        }
    }

    public void initQuestion(Long questionId) {
    }

    public void clearQuestion() {

    }
}
